package
{
	import flash.display.MovieClip;

	public class Ragdoll
	{
		public var snd:MovieClip;
		public var pList:Object;
		public var sList:Object;
		public var exploded:Boolean;
		public var pos:Vector2;
		public var oldpos:Vector2;

		public function Ragdoll(pos, rad, scale)
		{
			var _local2 = pos;
			var _local3 = rad;
			this.snd = Main.getInstance().gfx.CreateSprite("ragdollSoundMC", NinjaGraphicsSystem.LAYER_PLAYER);
			//
			this.pList = new Object();
			this.pList.b0 = new RagParticle(_local2.x, _local2.y, 2.5, 0.99, this.snd);
			this.pList.b1 = new RagParticle(_local2.x, _local2.y - _local3, 2.5, 0.995, this.snd);
			this.pList.h0 = new RagParticle(_local2.x + _local3, _local2.y - _local3, 2, 0.995, this.snd);
			this.pList.h1 = new RagParticle(_local2.x - _local3, _local2.y - _local3, 2, 0.99, this.snd);
			this.pList.f0 = new RagParticle(_local2.x + _local3, _local2.y + _local3, 3, 0.99, this.snd);
			this.pList.f1 = new RagParticle(_local2.x - _local3, _local2.y + _local3, 3, 0.995, this.snd);
			//
			this.sList = new Object();
			this.sList.armL = new RagStick(this.pList.b1.pos, this.pList.h1.pos, 0.26, 0.6, 40, "arm_m", scale, -1);
			this.sList.legL = new RagStick(this.pList.b0.pos, this.pList.f1.pos, 0.37, 0.6, 60, "leg_m", scale, 1);
			this.sList.body = new RagStick(this.pList.b0.pos, this.pList.b1.pos, 0.4, 0.8, 30, "body_m", scale, 1);
			this.sList.legR = new RagStick(this.pList.b0.pos, this.pList.f0.pos, 0.32, 0.6, 60, "leg_m", scale, 1);
			this.sList.armR = new RagStick(this.pList.b1.pos, this.pList.h0.pos, 0.2, 0.6, 40, "arm_m", scale, -1);
			this.exploded = false;
		}

		public function ReportCollisionVsWorld(x, y, nx, ny, t):void
		{
			var _local2 = this.pos;
			var o = this.oldpos;
			var vx = (_local2.x - o.x);
			var vy = (_local2.y - o.y);
			var dp = ((vx * nx) + (vy * ny));
			var bx = (dp * nx);
			var _local3 = dp * ny;
			var px = (vx - bx);
			var py = (vy - _local3);
			if (dp < 0)
			{
				if (dp < -3)
				{
					Main.getInstance().particles.SpawnRagBloodSpurt(_local2.x, _local2.y, -bx, -_local3);
					var n = (Main.getInstance().game.GetTime() % 3);
					if (n == 0)
					{
						this.snd.gotoAndPlay("hard1");
					}
					else if (n == 1)
					{
						this.snd.gotoAndPlay("hard2");
					}
					else if (n == 2)
					{
						this.snd.gotoAndPlay("hard3");
					}
				}
				else
				{
					if (dp < -2)
					{
						if ((Main.getInstance().game.GetTime() % 2) == 0)
						{
							this.snd.gotoAndPlay("med1");
						}
						else
						{
							this.snd.gotoAndPlay("med2");
						}
					}
					else if (dp < -1)
					{
						if ((Main.getInstance().game.GetTime() % 2) == 0)
						{
							this.snd.gotoAndPlay("soft1");
						}
						else
						{
							this.snd.gotoAndPlay("soft2");
						}
					}
					var temp = ((px * px) + (py * py));
					if (0.3 < temp)
					{
						Main.getInstance().particles.SpawnRagDust(this.pos, this.xw, px, py, temp);
					}
				}
				bx = bx * 1.4;
				_local3 = _local3 * 1.4;
			}
			else
			{
				_local3 = 0;
				bx = _local3;
			}
			_local2.x = _local2.x + x;
			_local2.y = _local2.y + y;
			o.x = o.x + ((x + bx) + (px * 0.15));
			o.y = o.y + ((y + _local3) + (py * 0.15));
			this.hit = true;
		}

		public function Destruct():void
		{
			var _local1;
			for (var _local3 in this.sList)
			{
				_local1 = this.sList[_local3];
				Main.getInstance().gfx.DestroyMC(_local1.mc);
				_local1.mc = null;
			}
		}

		public function Hide():void
		{
			var _local1 = this.sList;
			for (var _local2 in _local1)
			{
				_local1.mc.visible = false;
			}
		}

		public function Tick():void
		{
			var _local3 = this.pList;
			for (var i in _local3)
			{
				var _local1 = _local3[i].pos;
				var o = _local3[i].oldpos;
				var ox = o.x;
				var oy = o.y;
				var px = ((o.x = _local1.x));
				var py = ((o.y = _local1.y));
				var vx = (px - ox);
				var vy = (py - oy);
				var d = _local3[i].drag;
				_local1.x = _local1.x + (d * vx);
				_local1.y = _local1.y + ((d * vy) + 0.15);
				_local3[i].v = (vx * vx) + (vy * vy);
			}
			var sl = this.sList;
			for (var i in sl)
			{
				var _local2 = sl[i];
				var p0 = _local2.p0;
				var p1 = _local2.p1;
				var mn = _local2.minlen;
				var maax = _local2.maxlen;
				var dx = (p0.x - p1.x);
				var dy = (p0.y - p1.y);
				var clen = Math.sqrt((dx * dx) + (dy * dy));
				var d0 = 0;
				var d1 = 0;
				if (clen == 0)
				{
					continue;
				}
				else if (clen < mn)
				{
					d0 = (d1 = (clen - mn) / clen);
				}
				else if (maax < clen)
				{
					d0 = (d1 = (clen - maax) / clen);
				}
				else
				{
					_local2.curlen = clen;
					continue;
				}
				_local2.curlen = clen - d0;
				d0 = d0 * _local2.w0;
				d1 = d1 * _local2.w1;
				p0.x = p0.x - (dx * d0);
				p0.y = p0.y - (dy * d0);
				p1.x = p1.x + (dx * d1);
				p1.y = p1.y + (dy * d1);
			}
			for (var i in _local3)
			{
				var _local1 = _local3[i];
				if (_local1.v < 2)
				{
					Main.getInstance().CollideAABBvsTileMap(_local1);
				}
				else if (_local1.v < 3)
				{
					var tx = _local1.pos.x;
					var ty = _local1.pos.y;
					_local1.hit = false;
					Main.getInstance().CollideAABBvsTileMap(_local1);
					if (_local1.hit)
					{
						var cx = (0.5 * (tx + _local1.oldpos.x));
						var cy = (0.5 * (ty + _local1.oldpos.y));
						var vx = (tx - cx);
						var vy = (ty - cy);
						_local1.oldpos.x = _local1.oldpos.x - vx;
						_local1.oldpos.y = _local1.oldpos.y - vy;
						_local1.pos.x = cx;
						_local1.pos.y = cy;
						_local1.hit = false;
						Main.getInstance().CollideAABBvsTileMap(_local1);
						if (!_local1.hit)
						{
							_local1.pos.x = tx;
							_local1.pos.y = ty;
							_local1.oldpos.x = _local1.oldpos.x + vx;
							_local1.oldpos.y = _local1.oldpos.y + vy;
						}
					}
				}
				else
				{
					var tx = _local1.pos.x;
					var ty = _local1.pos.y;
					var a = 0.333333333333333;
					var b = 0.666666666666667;
					_local1.hit = false;
					Main.getInstance().CollideAABBvsTileMap(_local1);
					if (_local1.hit)
					{
						var cx = ((b * tx) + (a * _local1.oldpos.x));
						var cy = ((b * ty) + (a * _local1.oldpos.y));
						tx = _local1.pos.x;
						ty = _local1.pos.y;
						var vx = (tx - cx);
						var vy = (ty - cy);
						_local1.oldpos.x = _local1.oldpos.x - vx;
						_local1.oldpos.y = _local1.oldpos.y - vy;
						_local1.pos.x = cx;
						_local1.pos.y = cy;
						_local1.hit = false;
						Main.getInstance().CollideAABBvsTileMap(_local1);
						if (_local1.hit)
						{
							cx = (a * tx) + (b * _local1.oldpos.x);
							cy = (a * ty) + (b * _local1.oldpos.y);
							tx = _local1.pos.x;
							ty = _local1.pos.y;
							vx = tx - cx;
							vy = ty - cy;
							_local1.oldpos.x = _local1.oldpos.x - vx;
							_local1.oldpos.y = _local1.oldpos.y - vy;
							_local1.pos.x = cx;
							_local1.pos.y = cy;
							_local1.hit = false;
							Main.getInstance().CollideAABBvsTileMap(_local1);
							if (!_local1.hit)
							{
								_local1.pos.x = tx;
								_local1.pos.y = ty;
								_local1.oldpos.x = _local1.oldpos.x + vx;
								_local1.oldpos.y = _local1.oldpos.y + vy;
							}
						}
						else
						{
							_local1.pos.x = tx;
							_local1.pos.y = ty;
							_local1.oldpos.x = _local1.oldpos.x + vx;
							_local1.oldpos.y = _local1.oldpos.y + vy;
						}
					}
				}
			}
		}

		public function Explode():void
		{
			var pl = this.pList;
			var sl = this.sList;
			var _local2 = pl.b1;
			var t0 = new RagParticle(_local2.pos.x, _local2.pos.y, _local2.xw, _local2.drag, null);
			var t1 = new RagParticle(_local2.pos.x, _local2.pos.y, _local2.xw, _local2.drag, null);
			sl.armL.p0 = t0.pos;
			sl.armR.p0 = t1.pos;
			var _local3 = pl.b0;
			var t2 = new RagParticle(_local3.pos.x, _local3.pos.y, _local3.xw, _local3.drag, null);
			var t3 = new RagParticle(_local3.pos.x, _local3.pos.y, _local3.xw, _local3.drag, null);
			sl.legL.p0 = t2.pos;
			sl.legR.p0 = t3.pos;
			pl.t0 = t0;
			pl.t1 = t1;
			pl.t2 = t2;
			pl.t3 = t3;
			var force = 8;
			var _local1 = force * 0.5;
			var quarter_force = (force * 0.25);
			var rnd = Math.random;
			var r0 = ((rnd() * force) - _local1);
			var r1 = ((rnd() * force) - _local1);
			var r2 = ((rnd() * force) - _local1);
			var r3 = ((rnd() * force) - _local1);
			var r4 = ((rnd() * _local1) + _local1);
			var r5 = ((rnd() * _local1) + _local1);
			var r6 = ((rnd() * _local1) + _local1);
			var r7 = ((rnd() * _local1) + _local1);
			t0.oldpos.x = t0.oldpos.x - r0;
			t1.oldpos.x = t1.oldpos.x - r1;
			t2.oldpos.x = t2.oldpos.x - r2;
			t3.oldpos.x = t3.oldpos.x - r3;
			t0.oldpos.y = t0.oldpos.y + r4;
			t1.oldpos.y = t1.oldpos.y + r5;
			t2.oldpos.y = t2.oldpos.y + r6;
			t3.oldpos.y = t3.oldpos.y + r7;
			this.exploded = true;
			Main.getInstance().particles.SpawnBloodSpurt(t0.pos.x, t0.pos.y, r0, r4, 3);
			Main.getInstance().particles.SpawnBloodSpurt(t1.pos.x, t1.pos.y, r1, r5, 3);
			Main.getInstance().particles.SpawnBloodSpurt(t2.pos.x, t2.pos.y, r2, r6, 3);
			Main.getInstance().particles.SpawnBloodSpurt(t3.pos.x, t3.pos.y, r3, r7, 3);
		}

		public function UnExplode():void
		{
			var _local1 = this.pList;
			var _local2 = this.sList;
			_local2.armL.p0 = _local1.b1.pos;
			_local2.armR.p0 = _local1.b1.pos;
			_local2.legL.p0 = _local1.b0.pos;
			_local2.legR.p0 = _local1.b0.pos;
			_local1.t0 = null;
			_local1.t1 = null;
			_local1.t2 = null;
			_local1.t3 = null;
			this.exploded = false;
		}

		public function Activate():void
		{
			var _local1 = this.sList;
			for (var _local2 in _local1)
			{
				var temp = _local1[_local2].mc;
				temp.visible = true;
			}
		}

		public function Deactivate():void
		{
			var _local1 = this.sList;
			for (var _local2 in _local1)
			{
				var temp = _local1[_local2].mc;
				temp.visible = false;
			}
		}

		public function MimicMC(vx, vy, mc, facing, prevframe):void
		{
			var _local3 = mc;
			for (var i in this.pList)
			{
			}
			var sL = this.sList;
			if (facing < 0)
			{
				var dir = 1;
			}
			else
			{
				var dir = -1;
			}
			var temp;
			for (var i in sL)
			{
				temp = sL[i].mc;
				temp.scaleY = (sL[i].flip * dir) * Math.abs(temp.scaleY);
			}
			var _local1 = new Object();
			var pL = this.pList;
			var b0 = pL.b0;
			var b1 = pL.b1;
			var h0 = pL.h0;
			var h1 = pL.h1;
			var f0 = pL.f0;
			var f1 = pL.f1;
			_local1.x = _local3.shoulder.x;
			_local1.y = _local3.shoulder.y;
			_local3.localToGlobal(_local1);
			b1.pos.x = _local1.x;
			b1.pos.y = _local1.y;
			_local1.x = _local3.pelvis.x;
			_local1.y = _local3.pelvis.y;
			_local3.localToGlobal(_local1);
			b0.pos.x = _local1.x;
			b0.pos.y = _local1.y;
			_local1.x = _local3.handR.x;
			_local1.y = _local3.handR.y;
			_local3.localToGlobal(_local1);
			h0.pos.x = _local1.x;
			h0.pos.y = _local1.y;
			_local1.x = _local3.handL.x;
			_local1.y = _local3.handL.y;
			_local3.localToGlobal(_local1);
			h1.pos.x = _local1.x;
			h1.pos.y = _local1.y;
			_local1.x = _local3.footR.x;
			_local1.y = _local3.footR.y;
			_local3.localToGlobal(_local1);
			f0.pos.x = _local1.x;
			f0.pos.y = _local1.y;
			_local1.x = _local3.footL.x;
			_local1.y = _local3.footL.y;
			_local3.localToGlobal(_local1);
			f1.pos.x = _local1.x;
			f1.pos.y = _local1.y;
			_local3.gotoAndStop(prevframe);
			_local1.x = _local3.shoulder.x;
			_local1.y = _local3.shoulder.y;
			_local3.localToGlobal(_local1);
			b1.oldpos.x = _local1.x - vx;
			b1.oldpos.y = _local1.y - vy;
			_local1.x = _local3.pelvis.x;
			_local1.y = _local3.pelvis.y;
			_local3.localToGlobal(_local1);
			b0.oldpos.x = _local1.x - vx;
			b0.oldpos.y = _local1.y - vy;
			_local1.x = _local3.handR.x;
			_local1.y = _local3.handR.y;
			_local3.localToGlobal(_local1);
			h0.oldpos.x = _local1.x - vx;
			h0.oldpos.y = _local1.y - vy;
			_local1.x = _local3.handL.x;
			_local1.y = _local3.handL.y;
			_local3.localToGlobal(_local1);
			h1.oldpos.x = _local1.x - vx;
			h1.oldpos.y = _local1.y - vy;
			_local1.x = _local3.footR.x;
			_local1.y = _local3.footR.y;
			_local3.localToGlobal(_local1);
			f0.oldpos.x = _local1.x - vx;
			f0.oldpos.y = _local1.y - vy;
			_local1.x = _local3.footL.x;
			_local1.y = _local3.footL.y;
			_local3.localToGlobal(_local1);
			f1.oldpos.x = _local1.x - vx;
			f1.oldpos.y = _local1.y - vy;
			for (var i in this.pList)
			{
			}
		}

		public function Shove(x, y):void
		{
			var _local3 = y;
			for (var _local2 in this.pList)
			{
				this.pList[_local2].oldpos.x = this.pList[_local2].oldpos.x - (x * (Math.random() + 0.4));
				this.pList[_local2].oldpos.y = this.pList[_local2].oldpos.y - (_local3 * (Math.random() + 0.4));
			}
		}

		public function Shove_VertBias(fx, fy, px, py, midy, rad):void
		{
			var pL = this.pList;
			var b0 = pL.b0;
			var b1 = pL.b1;
			var h0 = pL.h0;
			var h1 = pL.h1;
			var f0 = pL.f0;
			var f1 = pL.f1;
			var _local2 = (py - midy) / rad;
			var near_bias = 0.8;
			var mid_bias = 0.4;
			var far_bias = 0.2;
			var wb0 = 1;
			var _local3 = 1;
			var wf = 1;
			if (_local2 < 0)
			{
				if (_local2 < -1)
				{
					wb0 = mid_bias;
					_local3 = near_bias;
					wf = far_bias;
				}
				else
				{
					_local2 = _local2 * -1;
					var i = (1 - _local2);
					wb0 = (i * near_bias) + (_local2 * mid_bias);
					_local3 = (i * mid_bias) + (_local2 * near_bias);
					wf = (i * mid_bias) + (_local2 * far_bias);
				}
			}
			else if (0 < _local2)
			{
				if (1 < _local2)
				{
					wb0 = mid_bias;
					wf = near_bias;
					_local3 = far_bias;
				}
				else
				{
					var i = (1 - _local2);
					wb0 = (i * near_bias) + (_local2 * mid_bias);
					wf = (i * mid_bias) + (_local2 * near_bias);
					_local3 = (i * mid_bias) + (_local2 * far_bias);
				}
			}
			else
			{
				_local3 = 0.4;
				wb0 = 0.4;
				wf = 0.4;
			}
			var _local1 = Math.random;
			b0.oldpos.x = b0.oldpos.x - ((_local1() + wb0) * fx);
			b0.oldpos.y = b0.oldpos.y - ((_local1() + wb0) * fy);
			b1.oldpos.x = b1.oldpos.x - ((_local1() + _local3) * fx);
			b1.oldpos.y = b1.oldpos.y - ((_local1() + _local3) * fy);
			var wf0 = (wf * (0.8 + (0.2 * _local1())));
			var wf1 = (wf * (0.9 + (0.1 * _local1())));
			f0.oldpos.x = f0.oldpos.x - ((_local1() + wf0) * fx);
			f0.oldpos.y = f0.oldpos.y - ((_local1() + wf0) * fy);
			f1.oldpos.x = f1.oldpos.x - ((_local1() + wf1) * fx);
			f1.oldpos.y = f1.oldpos.y - ((_local1() + wf1) * fy);
			var wh0 = (_local3 * (0.9 + (0.1 * _local1())));
			var wh1 = (_local3 * (0.8 + (0.2 * _local1())));
			h0.oldpos.x = h0.oldpos.x - ((_local1() + wh0) * fx);
			h0.oldpos.y = h0.oldpos.y - ((_local1() + wh0) * fy);
			h1.oldpos.x = h1.oldpos.x - ((_local1() + wh1) * fx);
			h1.oldpos.y = h1.oldpos.y - ((_local1() + wh1) * fy);
		}

		public function DrawDebug():void
		{
			Main.getInstance().static_rend.SetStyle(0, 16777215, 20);
			var _local3 = this.sList;
			var _local2;
			for (var i in _local3)
			{
				_local2 = _local3[i];
				Main.getInstance().static_rend.DrawLine(_local2.p0, _local2.p1);
			}
			var pl = this.pList;
			var _local1;
			for (var i in pl)
			{
				_local1 = pl[i];
				Main.getInstance().static_rend.DrawAABB(_local1.pos, _local1.xw, _local1.yw);
			}
		}

		public function Draw():void
		{
			var sL = this.sList;
			var denom = (Math.PI / 180);
			for (var i in sL)
			{
				var s = sL[i];
				var mc = s.mc;
				var p0 = s.p0;
				var p1 = s.p1;
				var _local2 = p1.x - p0.x;
				var _local1 = p1.y - p0.y;
				mc.x = p0.x;
				mc.y = p0.y;
				var len = Math.sqrt((_local2 * _local2) + (_local1 * _local1));
				mc.gotoAndStop(1 + Math.floor(100 * (len / s.maxlen)));
				_local2 = _local2 / len;
				_local1 = _local1 / len;
				var _local3 = 0;
				if (_local2 == 0)
				{
					if (_local1 < 0)
					{
						_local3 = -90;
					}
					else if (0 < _local1)
					{
						_local3 = 90;
					}
				}
				else if (_local1 == 0)
				{
					if (_local2 < 0)
					{
						_local3 = 180;
					}
					else
					{
						_local3 = 0;
					}
				}
				else
				{
					_local3 = Math.atan(_local1 / _local2) / denom;
					if (_local2 < 0)
					{
						_local3 = _local3 + 180;
					}
				}
				mc.rotation = _local3;
			}
		}
	}
}